| Conditions | 48 |
| Total Lines | 123 |
| Code Lines | 100 |
| Lines | 123 |
| Ratio | 100 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like date.matt.kruse.js ➔ getDateFromFormat often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | // =================================================================== |
||
| 183 | function getDateFromFormat(val,format) { |
||
| 184 | val=val+""; |
||
| 185 | format=format+""; |
||
| 186 | var i_val=0; |
||
| 187 | var i_format=0; |
||
| 188 | var c=""; |
||
| 189 | var token=""; |
||
| 190 | var token2=""; |
||
| 191 | var x,y; |
||
| 192 | var now=new Date(); |
||
| 193 | var year=now.getYear(); |
||
| 194 | var month=now.getMonth()+1; |
||
| 195 | var date=1; |
||
| 196 | var hh=now.getHours(); |
||
| 197 | var mm=now.getMinutes(); |
||
| 198 | var ss=now.getSeconds(); |
||
| 199 | var ampm=""; |
||
| 200 | |||
| 201 | while (i_format < format.length) { |
||
| 202 | // Get next token from format string |
||
| 203 | c=format.charAt(i_format); |
||
| 204 | token=""; |
||
| 205 | while ((format.charAt(i_format)==c) && (i_format < format.length)) { |
||
| 206 | token += format.charAt(i_format++); |
||
| 207 | } |
||
| 208 | // Extract contents of value based on format token |
||
| 209 | if (token=="yyyy" || token=="yy" || token=="y") { |
||
| 210 | if (token=="yyyy") { x=4;y=4; } |
||
| 211 | if (token=="yy") { x=2;y=2; } |
||
| 212 | if (token=="y") { x=2;y=4; } |
||
| 213 | year=_getInt(val,i_val,x,y); |
||
| 214 | if (year==null) { return 0; } |
||
| 215 | i_val += year.length; |
||
| 216 | if (year.length==2) { |
||
| 217 | if (year > 70) { year=1900+(year-0); } |
||
| 218 | else { year=2000+(year-0); } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | else if (token=="MMM"||token=="NNN"){ |
||
| 222 | month=0; |
||
| 223 | for (var i=0; i<MONTH_NAMES.length; i++) { |
||
| 224 | var month_name=MONTH_NAMES[i]; |
||
| 225 | if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) { |
||
| 226 | if (token=="MMM"||(token=="NNN"&&i>11)) { |
||
| 227 | month=i+1; |
||
| 228 | if (month>12) { month -= 12; } |
||
| 229 | i_val += month_name.length; |
||
| 230 | break; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | } |
||
| 234 | if ((month < 1)||(month>12)){return 0;} |
||
| 235 | } |
||
| 236 | else if (token=="EE"||token=="E"){ |
||
| 237 | for (var i=0; i<DAY_NAMES.length; i++) { |
||
| 238 | var day_name=DAY_NAMES[i]; |
||
| 239 | if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) { |
||
| 240 | i_val += day_name.length; |
||
| 241 | break; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | else if (token=="MM"||token=="M") { |
||
| 246 | month=_getInt(val,i_val,token.length,2); |
||
| 247 | if(month==null||(month<1)||(month>12)){return 0;} |
||
| 248 | i_val+=month.length;} |
||
| 249 | else if (token=="dd"||token=="d") { |
||
| 250 | date=_getInt(val,i_val,token.length,2); |
||
| 251 | if(date==null||(date<1)||(date>31)){return 0;} |
||
| 252 | i_val+=date.length;} |
||
| 253 | else if (token=="hh"||token=="h") { |
||
| 254 | hh=_getInt(val,i_val,token.length,2); |
||
| 255 | if(hh==null||(hh<1)||(hh>12)){return 0;} |
||
| 256 | i_val+=hh.length;} |
||
| 257 | else if (token=="HH"||token=="H") { |
||
| 258 | hh=_getInt(val,i_val,token.length,2); |
||
| 259 | if(hh==null||(hh<0)||(hh>23)){return 0;} |
||
| 260 | i_val+=hh.length;} |
||
| 261 | else if (token=="KK"||token=="K") { |
||
| 262 | hh=_getInt(val,i_val,token.length,2); |
||
| 263 | if(hh==null||(hh<0)||(hh>11)){return 0;} |
||
| 264 | i_val+=hh.length;} |
||
| 265 | else if (token=="kk"||token=="k") { |
||
| 266 | hh=_getInt(val,i_val,token.length,2); |
||
| 267 | if(hh==null||(hh<1)||(hh>24)){return 0;} |
||
| 268 | i_val+=hh.length;hh--;} |
||
| 269 | else if (token=="mm"||token=="m") { |
||
| 270 | mm=_getInt(val,i_val,token.length,2); |
||
| 271 | if(mm==null||(mm<0)||(mm>59)){return 0;} |
||
| 272 | i_val+=mm.length;} |
||
| 273 | else if (token=="ss"||token=="s") { |
||
| 274 | ss=_getInt(val,i_val,token.length,2); |
||
| 275 | if(ss==null||(ss<0)||(ss>59)){return 0;} |
||
| 276 | i_val+=ss.length;} |
||
| 277 | else if (token=="a") { |
||
| 278 | if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";} |
||
| 279 | else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";} |
||
| 280 | else {return 0;} |
||
| 281 | i_val+=2;} |
||
| 282 | else { |
||
| 283 | if (val.substring(i_val,i_val+token.length)!=token) {return 0;} |
||
| 284 | else {i_val+=token.length;} |
||
| 285 | } |
||
| 286 | } |
||
| 287 | // If there are any trailing characters left in the value, it doesn't match |
||
| 288 | if (i_val != val.length) { return 0; } |
||
| 289 | // Is date valid for month? |
||
| 290 | if (month==2) { |
||
| 291 | // Check for leap year |
||
| 292 | if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year |
||
| 293 | if (date > 29){ return 0; } |
||
| 294 | } |
||
| 295 | else { if (date > 28) { return 0; } } |
||
| 296 | } |
||
| 297 | if ((month==4)||(month==6)||(month==9)||(month==11)) { |
||
| 298 | if (date > 30) { return 0; } |
||
| 299 | } |
||
| 300 | // Correct hours value |
||
| 301 | if (hh<12 && ampm=="PM") { hh=hh-0+12; } |
||
| 302 | else if (hh>11 && ampm=="AM") { hh-=12; } |
||
| 303 | var newdate=new Date(year,month-1,date,hh,mm,ss); |
||
| 304 | return newdate; |
||
| 305 | } |
||
| 306 | |||
| 336 |